import logging
import time
import requests
import json

port = 8080


def init_monitor(image_name: str, client, udp_port: int):
    client.client.containers.run(image_name, detach=True, environment=[
        'UDP_PORT=' + str(udp_port),
    ], ports={'%d/tcp' % port: port})


def set_monitor(raw_payload: list):
    while True:
        try:
            payload = {
                'total': len(raw_payload),
                'items': raw_payload
            }
            requests.post(url='http://127.0.0.1:%d/api/satellite/list' % port, data=json.dumps(payload))
            break
        except:
            logging.error("Monitor doesn't response.Retry...")
            time.sleep(1)


